package cn.nuc.controller.sensorfire.back;

import cn.nuc.pojo.entity.ResponseData;
import cn.nuc.pojo.sensorfire.Fire;
import cn.nuc.pojo.sensorfire.Sensor;
import cn.nuc.service.sensorfire.IFireService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/system/fire/back")
@Api(tags = "历年火灾信息")
public class FireController {
    @Autowired
    private IFireService fireService;
    @GetMapping("/fireList")
    @ApiOperation(value = "查看历年火灾信息")
    public ResponseData<Fire> getAll(){
        List<Fire> fireList = fireService.getAllFire();
        if(fireList != null){
            return ResponseData.success().putDataVule("fireList" , fireList);
        }
        return ResponseData.serverInternalError();
    }
    @PutMapping("/saveOrUpdate")
    @ApiOperation(value = "添加或修改火灾信息")
    @ApiImplicitParam(value = "要添加或修改的火灾对象" , name = "fire" , required = true , dataType = "Fire" , paramType = "body")
    public ResponseData<Fire> saveOrUpdate(@RequestBody Fire fire){
        boolean update = fireService.saveOrUpdate(fire);
        if(update){
            return ResponseData.success().putDataVule("saveOrUpdate" , true);
        }
        return ResponseData.serverInternalError();
    }
//    模糊查询
    @GetMapping("/fireByTime")
    @ApiOperation(value = "根据时间查询火灾信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "开始时间" , name = "begin" , required = true , dataType = "String" , paramType = "query"),
            @ApiImplicitParam(value = "结束时间" , name = "end" , required = true , dataType = "String" , paramType = "query")
    })
    public ResponseData<Fire> findFireByDate(String begin , String end){
        List<Fire> fireList = fireService.getSomeFire(begin, end);
        if(fireList != null){
            return  ResponseData.success().putDataVule("fireListByTime" , fireList);
        }
        return ResponseData.serverInternalError();
    }
    @GetMapping("/getOne")
    public ResponseData<Sensor> getOneById(Integer id){
        List<Fire> sensors = fireService.list(new QueryWrapper<Fire>().eq("id", id));
        if(sensors != null){
            return ResponseData.success().putDataVule("fire" , sensors.get(0));
        }
        return ResponseData.serverInternalError();
    }
}
